/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package domain.authenticationManagement.controllers;

import domain.authenticationManagement.exceptions.InvalidLoginException;
import domain.userManagement.models.User;
import domain.userManagement.models.UserList;
import services.security.EncryptedPasswordGenerator;

/**
 *
 * @author peterfrem
 */
public class AuthenticationManager {
    
    public static boolean loginUser(String username, String plainPassword) 
	    throws InvalidLoginException{
	
	UserList users = UserList.getInstance();
	
	String encryptedPassword = EncryptedPasswordGenerator.encryptPassword(plainPassword);
	
	//if user exists
	if(users.userExists(username)){
	    User user = users.getUser(username);
	    
	    //if password correct
	    if(user.getPassword().equals(encryptedPassword)){
		return true;
	    }else{//password not correct
		
		throw new InvalidLoginException();
	    }//end if password correct
	    
	}else{//userdoesn't exist
	    throw new InvalidLoginException();
	}//end if user exists
    }
    
}
